package com.xdclass.mysql.controller;

import com.google.common.collect.Lists;
import com.xdclass.mysql.constant.RestItemResult;
import com.xdclass.mysql.dao.UserDao;
import com.xdclass.mysql.domain.User;
import com.xdclass.mysql.persist.mapper.custom.CustomUserMapper;
import com.xdclass.mysql.persist.mapper.generated.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author daniel
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserDao userDao;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CustomUserMapper customUserMapper;

    @RequestMapping("/query")
    private RestItemResult<User> queryById(){
        RestItemResult<User> userRestItemResult = new RestItemResult<>();
        User locate = userDao.locate(1L);
        userRestItemResult.setItem(locate);
        return userRestItemResult;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestItemResult<User> add(@RequestBody User user) {
        RestItemResult<User> result = new RestItemResult<>();
        userDao.add(user);
        if (user != null) {
            result.setItem(user);
            result.setResult("success");
        } else {
            result.setMessage("新增用户失败");
            result.setResult("failure");
        }
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RestItemResult<User> update(@RequestBody User user) {
        RestItemResult<User> result = new RestItemResult<>();
        userDao.update(user);
        if (user != null) {
            result.setItem(user);
            result.setResult("success");
        } else {
            result.setMessage("修改用户失败");
            result.setResult("failure");
        }
        return result;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public RestItemResult<User> delete(@PathVariable Long id) {
        RestItemResult<User> result = new RestItemResult<>();
        if (userDao.delete(id)) {
            result.setResult("success");
        } else {
            result.setMessage("删除用户失败");
            result.setResult("failure");
        }
        return result;
    }

    @RequestMapping(value = "/locate/{id}", method = RequestMethod.GET)
    public RestItemResult<User> locate(@PathVariable Long id) {
        RestItemResult<User> result = new RestItemResult<>();
        User user = userDao.locate(id);
        if (user != null) {
            result.setItem(user);
            result.setResult("success");
        } else {
            result.setMessage("查询用户失败");
            result.setResult("failure");
        }
        return result;
    }


    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public RestItemResult<com.xdclass.mysql.model.User> get(@PathVariable Long id) {
        RestItemResult<com.xdclass.mysql.model.User> result = new RestItemResult<>();
        com.xdclass.mysql.model.User user = userMapper.selectByPrimaryKey(id);
        if (user != null) {
            result.setItem(user);
            result.setResult("success");
        } else {
            result.setMessage("查询用户失败");
            result.setResult("failure");
        }
        return result;
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public RestItemResult<com.xdclass.mysql.model.User> get(String name,String company) {
        RestItemResult<com.xdclass.mysql.model.User> result = new RestItemResult<>();
        com.xdclass.mysql.model.User user = customUserMapper.selectByNameAndCompany(name,company);
        if (user != null) {
            result.setItem(user);
            result.setResult("success");
        } else {
            result.setMessage("查询用户失败");
            result.setResult("failure");
        }
        return result;
    }

    @RequestMapping(value = "/batchInsert", method = RequestMethod.GET)
    public RestItemResult<Integer> batchInsert() {
        int num = 1000;
        List<com.xdclass.mysql.model.User> userList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            com.xdclass.mysql.model.User u = new com.xdclass.mysql.model.User();
            u.setName("daniel"+i);
            u.setCompany("xdclass"+i);
            userList.add(u);
        }
        List<List<com.xdclass.mysql.model.User>> partition = Lists.partition(userList, 200);
        partition.forEach(users -> customUserMapper.batchInsert(users));
        RestItemResult<Integer> integerRestItemResult = new RestItemResult<>();
        integerRestItemResult.setItem(100);
        return integerRestItemResult;
    }



}
